package com.link.wseller.serive.impl;

import com.link.wseller.domain.OrderDetail;
import com.link.wseller.domain.OrderMaster;
import com.link.wseller.domain.ProductInfo;
import com.link.wseller.enums.OrderStatusEnum;
import com.link.wseller.enums.PayStatusEnum;
import com.link.wseller.enums.SellNum;
import com.link.wseller.exception.SellException;
import com.link.wseller.mapper.OrderMasterMapper;
import com.link.wseller.mapper.impl.OrderDetailMapperImpl;
import com.link.wseller.mapper.impl.OrderMasterMapperImpl;
import com.link.wseller.serive.OrderMasterService;
import com.link.wseller.utils.KeyUtils;
import dto.CartDTO;
import dto.OrderDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class OrderMasterServiceImpl implements OrderMasterService {

    @Autowired
    private OrderMasterMapperImpl orderMasterMapper;

    @Autowired
    private ProductInfoServiceImpl productInfoService;

    @Autowired
    private OrderDetailMapperImpl orderDetailMapper;

    @Override
    @Transactional
    public int createOrder(OrderDTO orderDTO) {

        String orderId = KeyUtils.genUniqueKey();
        BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);


        //1. 查询商品 (数量,价格)
        for (OrderDetail orderDetail: orderDTO.getOrderDetailList()){

            ProductInfo productInfo = productInfoService.findOne(orderDetail.getProductId());
            if (productInfo == null){
                throw new SellException(SellNum.PRODUCT_NOT_EXIST);
            }

            //2. 计算总价
            orderAmount = productInfo.getProductPrice()
                    .multiply(new BigDecimal(orderDetail.getProductQuantity()))
                    .add(orderAmount);

            //3. 写入详情数据库 （ordermaster orderDetail）
            orderDetail.setDetailId(KeyUtils.genUniqueKey());
            orderDetail.setOrderId(orderId);
            BeanUtils.copyProperties(productInfo, orderDetail);
            orderDetailMapper.insert(orderDetail);
        }

        //4. 写入订单数据库
        OrderMaster orderMaster = new OrderMaster();
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderId(orderId);
        orderMaster.setOrderAmount(orderAmount);
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMasterMapper.insert(orderMaster);

        //5. 扣库存

        List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream().map(orderDetail ->
                new CartDTO(orderDetail.getProductId(),
                        orderDetail.getProductQuantity()))
                .collect(Collectors.toList());
        productInfoService.decreaseStock(cartDTOList);
        return 1;
    }

    @Override
    public List<OrderDTO> findOrderByOpenId(String openId) {
        return null;
    }

    @Override
    public OrderDTO findOrderByOrderId(String orderId, Integer page, Integer size) {
        return null;
    }

    @Override
    public int cancel(OrderDTO orderDTO) {
        return 0;
    }

    @Override
    public int finish(OrderDTO orderDTO) {
        return 0;
    }

    @Override
    public int paidOrder(OrderDTO orderDTO) {
        return 0;
    }
}
